﻿namespace KinonekoSoftware.CodariaDB.Core
{
    public abstract class _Delta<T> : _Delta where T : DeltaObject
    {
        protected internal override void OnDatabaseOpeningInternal(RepositoryOpenContext context)
        {
            var logger     = context.Logger;
            var repository = context.Repository;
            InsideDB = repository.Get<T>(GetCollectionName());
            logger.Debug($"正在打开数据表:{GetCollectionName()}");
        }

        protected internal override void OnDatabaseClosingInternal(ILogger logger)
        {
            InsideDB = null;
            logger.Debug($"关闭数据表，{GetCollectionName()}");
        }

        protected override void OnDatabaseOpening(RepositoryOpenContext context)
        {

        }

        protected override void OnDatabaseClosing(ILogger logger)
        {
        }


        public abstract string GetCollectionName();
        public ILiteCollection<T> InsideDB { get; private set; }
    }

    public abstract class _Delta : Engine
    {

        protected void Modified(DeltaObject deltaObj)
        {
            if (deltaObj.SyncFlags == SyncResult.Tracked)
            {
                deltaObj.SyncFlags = SyncResult.Expired;
            }

            Modified();
        }

    }
}